package demo.utils;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
import demo.model.Account;

/**
 * Created by IntelliJ IDEA.
 * User: zigang.wang
 * Date: 12-5-17
 * Time: 上午10:15
 * To change this template use File | Settings | File Templates.
 */
public class XStreamUtil {
    static XStream xstream = new XStream(new JettisonMappedXmlDriver());

    static {
        xstream.setMode(XStream.NO_REFERENCES);
    }

    public static String  objectToString(Object o) {
        xstream.alias("Account", Account.class);
        return xstream.toXML(o);
    }
    
    public static <T> T StringToObject(String json){
        return (T) xstream.fromXML(json);
    } 

    public static void main(String[] args) {
        Account account = new Account();
        account.setId(1);
        account.setName("test");
        String ss = objectToString(account);
        System.out.println(ss);
        Account a = StringToObject(ss);
        System.out.println(a.getName());
        
    }
}
